package com.itheima.web.manager;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.autoconfig.oss.OssTemplate;
import com.itheima.domain.mongo.Questionnaire;
import com.itheima.service.db.ReportService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.OptionService;
import com.itheima.service.mongo.QuestionService;
import com.itheima.service.mongo.QuestionnaireService;
import com.itheima.vo.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class SoulManager {
    @DubboReference
    private QuestionnaireService questionnaireService;
    @DubboReference
    private ReportService reportService;
    @DubboReference
    private QuestionService questionService;
    @DubboReference
    private OptionService optionService;
    @DubboReference
    private UserInfoService userInfoService;

    @Autowired
    private OssTemplate ossTemplate;


    public ResponseEntity findPage(Integer pageNum, Integer pageSize) {
        PageBeanVo pageBeanVo = questionnaireService.findPage(pageNum, pageSize);
        Page<QuestionnaireVo> page = new Page<>(pageNum, pageSize, pageBeanVo.getCounts());
        List<Questionnaire> questionnaires = (List<Questionnaire>) pageBeanVo.getItems();
        ArrayList<QuestionnaireVo> records = new ArrayList<>();
        for (Questionnaire questionnaire : questionnaires) {
            QuestionnaireVo questionnaireVo = new QuestionnaireVo();
            BeanUtil.copyProperties(questionnaire,questionnaireVo);
            records.add(questionnaireVo);
        }
        page.setRecords(records);
        return ResponseEntity.ok(page);
    }

    public ResponseEntity findById(String id) {
        Questionnaire questionnaire = questionnaireService.findById(new ObjectId(id));
        QuestionnaireVo questionnaireVo = new QuestionnaireVo();
        BeanUtil.copyProperties(questionnaire,questionnaireVo);
        return ResponseEntity.ok(questionnaireVo);
    }

    public ResponseEntity save(Questionnaire questionnaire) {
        questionnaireService.save(questionnaire);
        return ResponseEntity.ok(null);
    }

    public ResponseEntity uploadFile(MultipartFile files) throws IOException {
        String cover = ossTemplate.upload(files.getOriginalFilename(), files.getInputStream());
        return ResponseEntity.ok(cover);
    }

    public ResponseEntity deleteByIds(String[] ids) {
        ArrayList<ObjectId> obejctIds = new ArrayList<ObjectId>();
        for (String id : ids) {
            obejctIds.add(new ObjectId(id));
        }
        questionnaireService.deleteByIds(obejctIds);
        return ResponseEntity.ok(null);
    }
}
